<?php
namespace app\farm\controller;
use app\ApiRest;

use app\farm\model\Config;
use app\farm\model\FinanceWater;
use app\farm\model\User;

use app\farm\model\Wallet;
use app\shop\model\DistributionCash;
use app\shop\model\DistributionList;

use think\App;
use think\facade\Db;
use think\Request;


class IndexReseller extends ApiRest
{

    protected $model;

    protected $user_model;

    protected $cash_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new DistributionList();

        $this->user_model  = new User();

        $this->cash_model  = new DistributionCash();

        $dis = [

            'status' => 2,

            'user_id'=> $this->getUserId()
        ];

        $reseller = $this->model->dataInfo($dis);

        if(empty($reseller)){

            $this->errorMsg('你还不是分销商');
        }


    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-22 13:53
     * @功能说明:分销中心
     */
    public function resellerInfo(){

        $user = $this->user_model->dataInfo(['id'=>$this->getUserId()],'id,nickName,avatarUrl,fx_code,fx_cash');

        if(empty($user['fx_code'])){

            $user['fx_code'] = getCardCode();

            $this->user_model->dataUpdate(['id'=>$user['id']],['fx_code'=>$user['fx_code']]);

        }

        $dis = [

            'user_id' => $this->getUserId(),

            'status'  => 1
        ];

        $user['notreceived_cash'] = $this->cash_model->where($dis)->sum('cash');

        $wallet_model = new Wallet();

        $user['wallet_price'] = $wallet_model->where(['user_id'=>$this->getUserId(),'type'=>5])->where('status','in',[1,2])->sum('pay_price');

        return $this->success($user);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-27 18:00
     * @功能说明:订单列表
     */
    public function orderList(){

        $input = $this->_param;

        $dis[] = ['user_id','=',$this->getUserId()];

        if(!empty($input['status'])){

            $dis[] = ['status','=',$input['status']];

        }else{

            $dis[] = ['status','>',-1];
        }

        if(!empty($input['type'])){

            $dis[] = ['type','=',$input['type']];
        }

        $data = $this->cash_model->dataList($dis);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['source_info'] = $this->user_model->where(['id'=>$v['source_id']])->field('nickName,avatarUrl')->find();

                $v['order_price'] = array_sum(array_column($v['order_goods'],'pay_price'));

            }

        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-28 14:23
     * @功能说明:我的团队
     */
    public function myTeam(){

        $input = $this->_param;

        $data = $this->model->myTeam($this->getUserId(),$input['type']);

        $data['one_count'] = $this->model->teamCount($this->getUserId());

        $data['two_count'] = $this->model->teamCount($this->getUserId(),2);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-12-30 11:15
     * @功能说明:申请提现
     */
    public function applyWallet(){

        $input = $this->_input;

        $wallet_model = new Wallet();

        $user = $this->user_model->dataInfo(['id'=>$this->getUserId()]);

        if($input['apply_price']>$user['fx_cash']){

            $this->errorMsg('余额不足');
        }

        Db::startTrans();

        $insert = [

            'uniacid'    => $this->_uniacid,

            'user_id'    => $this->getUserId(),

            'order_code' => orderCode(),

            'farmer_id'  => 0,

            'pay_price'  => $input['apply_price'],

            'text'       => $input['text'],

            'true_price' => round($input['apply_price'],2),

            'balance'    => 100,

            'type'       => 5
        ];
        //发起提现
        $res = $wallet_model->dataAdd($insert);

        if($res==0){

            Db::rollback();

            $this->errorMsg('申请失败');

        }

        $res = $this->user_model->dataUpdate(['id'=>$this->getUserId(),'fx_cash'=>$user['fx_cash']],['fx_cash'=>$user['fx_cash']-$input['apply_price']]);

        if($res==0){

            Db::rollback();

            $this->errorMsg('申请失败');

        }

        Db::commit();

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-28 15:02
     * @功能说明:提现记录
     */
    public function walletList(){

        $input = $this->_param;

        $dis = [

            'user_id' => $this->getUserId(),

            'type'    => 5
        ];

        if(!empty($input['status'])){

            $dis['status'] = $input['status'];
        }

        $wallet_model = new Wallet();

        $data = $wallet_model->dataList($dis);

        return $this->success($data);
    }










}
